package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.service.CompositeGoodsService
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("compositeGoods")
class CompositeGoodsController(
    private val compositeGoodsService: CompositeGoodsService
) {

    data class AddCompositeGoodsRequest(
        val name: String,
        val price: BigDecimal,
        val note: String? = null,
        val items: List<Item> = emptyList(),
    ) {
        data class Item(
            val goodsId: Int? = null,
            val subGoodsId: Int? = null,
            val amount: Int
        )
    }

    @PostMapping
    fun add(@RequestBody request: AddCompositeGoodsRequest): Int {
        val goods = compositeGoodsService.createCompositeGoods(request)
        return goods.id
    }

    @PutMapping("{id}")
    fun update(
        @PathVariable id: Int,
        @RequestBody request: AddCompositeGoodsRequest
    ) {
        compositeGoodsService.updateCompositeGoods(id, request)
    }

    @GetMapping("{id}")
    fun getCompositeGoods(@PathVariable id: Int):CompositeGoodsDetail{
        return this.compositeGoodsService.getCompositeGoodsDetail(id)
    }

    data class CompositeGoodsDetail(
        val id: Int,
        val name: String,
        val price: BigDecimal,
        val note: String? = null,
        val barcode: String,
        val items: List<Item>,
    ){
        data class Item(
            val goodsId: Int ,
            val subGoodsId: Int? = null,
            val amount: Int,
            val barcode: String,
            val name: String,
            val subGoodsName: String? = null,
            val subGoodsNumber: String? = null,
        )
    }

}